package com.ruoyi.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.domain.OrdersDTO;
import com.ruoyi.domain.SubscribeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.domain.Points;
import com.ruoyi.service.IPointsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 积分Controller
 *
 * @author fst
 * @date 2025-04-14
 */
@RestController
@RequestMapping("/points")
public class PointsController extends BaseController
{
    @Autowired
    private IPointsService pointsService;

//    /**
//     * 查询积分列表
//     */
//    @RequiresPermissions("points:points:list")
//    @GetMapping("/list")
//    public TableDataInfo list(Points points)
//    {
//        startPage();
//        List<Points> list = pointsService.selectPointsList(points);
//        return getDataTable(list);
//    }

    /**
     * 导出积分列表
     */
    @RequiresPermissions("points:points:export")
    @Log(title = "积分", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Points points)
    {
        List<Points> list = pointsService.selectPointsList(points);
        ExcelUtil<Points> util = new ExcelUtil<Points>(Points.class);
        util.exportExcel(response, list, "积分数据");
    }

    /**
     * 获取积分详细信息
     */
    @RequiresPermissions("points:points:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pointsService.selectPointsById(id));
    }

    /**
     * 新增积分
     */
    @Log(title = "积分", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Points points)
    {
        return toAjax(pointsService.insertPoints(points));
    }

    /**
     * 修改积分
     */
    @RequiresPermissions("points:points:edit")
    @Log(title = "积分", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Points points)
    {
        return toAjax(pointsService.updatePoints(points));
    }

    /**
     * 删除积分
     */
    @RequiresPermissions("points:points:remove")
    @Log(title = "积分", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pointsService.deletePointsByIds(ids));
    }

    /**
     * 根据用户id查询积分
     */
    @RequiresPermissions("points:points:list")
    @GetMapping("/getPointsByUserId")
    public AjaxResult getPointsByUserId(@RequestParam("userId") Long userId){
            return success(pointsService.getPointsByUserId(userId));
    }
    /**
     * 订阅，扣减积分，增加到subscribe表中，增加到order表中，然后同步所有的订阅给作者。
     */
    @PutMapping("/subscribe")
    public AjaxResult subscribe(@RequestBody SubscribeDTO subscribeDTO){
        return AjaxResult.success(pointsService.subscribe(subscribeDTO));
    }

    /**
     * 查询出该用户需要订阅的列表（删除那些已经订阅的）
     */
    @GetMapping("/getSubscribeList")
    public AjaxResult getSubscribeList(@RequestParam("userId") Long userId,@RequestParam("title") String title) {

        return AjaxResult.success(pointsService.getSubscribeList(userId,title));
    }

    /**
     * 打赏月票  查询该用户积分是否足够，如果足够，扣减积分，在小说表中增加月票
     *  写入order表中，存流水
     */
    @PutMapping("/pass")
    public AjaxResult pass(@RequestBody OrdersDTO ordersDTO){
        return AjaxResult.success(pointsService.pass(ordersDTO));
    }

    /**
     * 计算作者的积分
     */
    @PostMapping("/calculatePoints")
    public AjaxResult calculatePoints(@RequestParam("userId") Long userId){
        return AjaxResult.success(pointsService.calculatePoints(userId));
    }
}
